package com.chpro.http.yixin.send;

import com.chpro.common.DateUtil;
import com.chpro.common.JSONUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

import java.io.Writer;

/**
 * 回复文本消息
 * Created by Administrator on 14-9-25.
 */
@XStreamAlias("xml")
public class ResTxtMsgModel {
    @XStreamAlias("ToUserName")
    private String toUserName;
    @XStreamAlias("FromUserName")
    private String fromUserName;
    @XStreamAlias("CreateTime")
    private String createTime;
    @XStreamAlias("MsgType")
    private String msgType;
    @XStreamAlias("Content")
    private String content;

    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    public String getFromUserName() {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName) {
        this.fromUserName = fromUserName;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toXml(ResTxtMsgModel resTxtMsgModel){
        XStream xstream = new XStream(new XppDriver() {
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out) {
                    // 对所有xml节点的转换都增加CDATA标记
                    boolean cdata = true;

                    @SuppressWarnings("unchecked")
                    public void startNode(String name, Class clazz) {
                        super.startNode(name, clazz);
                    }

                    protected void writeText(QuickWriter writer, String text) {
                        if (cdata) {
                            writer.write("<![CDATA[");
                            writer.write(text);
                            writer.write("]]>");
                        } else {
                            writer.write(text);
                        }
                    }
                };
            }
        });
        xstream.processAnnotations(ResTxtMsgModel.class);
        return xstream.toXML(resTxtMsgModel);
    }


    public static void main(String[] args){
        ResTxtMsgModel resTxtMsgModel = new ResTxtMsgModel();
        resTxtMsgModel.setToUserName("111");   //设置收接方的帐号
        resTxtMsgModel.setFromUserName("222");   //公众帐号ID
        resTxtMsgModel.setCreateTime(DateUtil.getCurrentDatetime()); //消息创建时间
        resTxtMsgModel.setMsgType("text");  //设置回复的消息类型
        resTxtMsgModel.setContent("这是一个普通的文本消息");
        String result = resTxtMsgModel.toXml(resTxtMsgModel);
        System.out.println(result);
    }
}
